package edu.ucsc.mit2008.swan.editor.coreUI.internal.presentation.wizard.page;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.ucsc.mit2008.swan.editor.core.presentation.model.IModelChangedListener;
import edu.ucsc.mit2008.swan.editor.core.presentation.model.ISwanPresentation;

public class NewPresentationWizardFirstPageUI extends Composite {
	
	private Label nameLabel;
	private Text nameText;

	private Label templateLabel;
	private Text templateText;
	private Button templateBrowseButton;				
	
	private ISwanPresentation presntationModel;
	
	private List<IModelChangedListener> listeners = new ArrayList<IModelChangedListener>();
	
	public NewPresentationWizardFirstPageUI(Composite parent, int style) {
		super(parent, style);
		createDetailsComposite(this, style);
	}

	public void modelChanged(ISwanPresentation model){
		this.presntationModel = model;
	}
	
	public void addModelChangedListener(IModelChangedListener listener){
		listeners.add(listener);
	}
	
	private Composite createDetailsComposite(Composite container, int style) {
		GridLayoutFactory.fillDefaults().numColumns(1).applyTo(container);
		Group generalGroup = new Group(container, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(generalGroup);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).applyTo(generalGroup);
		nameLabel = new Label(generalGroup, SWT.NONE);
		nameLabel.setText("Name");
		GridDataFactory.fillDefaults().applyTo(nameLabel);
		nameText = new Text(generalGroup, SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, false).applyTo(nameText);
		nameText.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				presntationModel.setName(nameText.getText());
				noifyListener();
			}
		});
		
		Group templateGroup = new Group(container, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).applyTo(templateGroup);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(templateGroup);
		templateLabel = new Label(templateGroup, SWT.NONE);
		templateLabel.setText("Template");
		GridDataFactory.fillDefaults().applyTo(templateLabel);
		templateText = new Text(templateGroup, SWT.BORDER);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).span(1, SWT.DEFAULT).grab(true, false).applyTo(templateText);
		templateBrowseButton = new Button(templateGroup, SWT.PUSH);
		templateBrowseButton.setText("Select");
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).applyTo(templateBrowseButton);
		templateBrowseButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
			
		});
		return container;
	}

	private void noifyListener(){
		for(IModelChangedListener listener:listeners){
			listener.modelChanged(presntationModel);
		}
	}
}
